﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Borton.Data;


namespace Borton.Beans
{
    class JoinBean_ : Bean
    {
        protected String relationReference;
        protected Type relation;
        protected Bean relationBean;
        protected Bean bean = null;

        public JoinBean_(String relationReference, Type relation)
            : base()
        {
            this.relationReference = relationReference;
            this.relation = relation;
        }

        private void initBean()
        {
            if (bean == null)
            {
                relationBean = (Bean)Activator.CreateInstance(relation);
                bean = ((Identifier)relationBean.getField(relationReference)).getBean();
            }
        }

        public override String getTableName()
        {
            initBean();
            return relationBean.getTableName();
        }

        public override Field[] getFields()
        {
            //TODO: join
            initBean();
            return bean.getFields();
        }

        public override Bean[] getReferringBeans()
        {
            //TODO: join
            initBean();
            return bean.getReferringBeans();
        }

        public override Field getField(String columnName)
        {
            initBean();
            return relationBean.getField(columnName);
        }

        public override Field getId()
        {
            initBean();
            return relationBean.getId();
        }

        public override String getDisplayName()
        {
            initBean();
            return relationBean.getDisplayName();
        }

        public override void saveState()
        {
            bean.saveState();
            //relationBean.saveState();
        }

        public override void delete()
        {
            DataAccess.delete_(relationBean);
            DataAccess.delete_(bean);
        }

        public override void load()
        {
            DataAccess.load_(relationBean);
            bean.getId().setValue(relationBean.getField(relationReference).getValueForSave());
            DataAccess.load_(bean);
        }

        public override void save()
        {
            DataAccess.save_(bean);
            relationBean.getField(relationReference).setValue(bean.getId().getValueForSave());
            DataAccess.save_(relationBean);
        }

        public override Bean[] getAll()
        {
            Bean[] relations = DataAccess.getAll__(relationBean);
            Bean[] beans = new Bean[relations.Length];
            for (int i = 0; i < relations.Length; i++)
            {
                beans[i] = ((Identifier)relations[i].getField(relationReference)).getBean();
            }
            return beans;
        }

        public override object Clone()
        {
            return new JoinBean_(relationReference, relation);
        }

    }
}

